<?php
require_once(LIB_DIR."SmartyImpl.class.php");

/**
 * Base class which responsible for page rendering
 */
class View{
	protected $smartyImpl;
	protected $TEMPLATES = array();
	protected $MAIN_TEMPLATE;
	protected $MAIN_JSON_TEMPLATE;
	protected $POPUP_TEMPLATE;
	protected $renderType = VS_NORMAL;
	protected $data;

	function __construct() {
		$lang = LANGUAGE_ENGLISH;
		$location = LOCATION_US;

		if(isset($_SESSION['lang'])) {
			$lang = $_SESSION['lang'];
		}

		if(isset($_SESSION['location'])) {
			$location = $_SESSION['location'];
		}

		$this->data = array();

		$this->smartyImpl = new SmartyImpl($lang);
		$this->MAIN_TEMPLATE = 'main.tpl';
		$this->MAIN_JSON_TEMPLATE = 'main_json.tpl';
		$this->POPUP_TEMPLATE = 'popup.tpl';

		$this->assign('HOST_URL',getHostUrl());
	}

	public function setRenderType($type) {
		$this->renderType = $type;
	}

	public function getRenderType() {
		return $this->renderType;
	}

	public function setTemplate($template) {
		unset($this->TEMPLATES);
		$this->TEMPLATES[] = $template;
	}

	public function setProperty($name, $property) {
		$this->data[$name] = $property;
	}

	public function setProperties($properties) {
		foreach($properties as $key => $value) {
			$this->setProperty($key, $value);
		}
	}

	public function setTitle($title) {
		$this->setProperty('TITLE',$title);
	}

	public function show() {
		$this->assign('sid',session_id());
		$this->prepareData();
		switch($this->getRenderType()) {
			case VS_NORMAL:
				$this->showMain($this->TEMPLATES[0]);
				break;
			case VS_PLUGIN:
				$this->showBlock($this->TEMPLATES[0]);
				break;
			case VS_JSON_NORMAL :
				$this->showMainJson($this->TEMPLATE[0]);
				break;
			default:
				die('unsupported render type.');
				break;
		}
	}

	public function getContent() {
		return $this->smartyImpl->fetch($this->TEMPLATES[0]);
	}

	protected function prepareData() {
		foreach($this->data as $smarty_name => $smarty_value) {
			$this->assign($smarty_name,$smarty_value);
		}
	}

	protected function showMain($template) {
		$content = $this->smartyImpl->fetch($template);
		$this->assign('CONTENT',$content);
		//$this->assign('sid',session_id());
		$this->assign('uid',$this->GetActiveUserId());
		$this->smartyImpl->display($this->MAIN_TEMPLATE);
	}

	protected function showMainJson($template) {
		$content = $this->smartyImpl->fetch($template);
		$this->assign('CONTENT',$content);
		$this->assign('uid',$this->GetActiveUserId());
		$this->smartyImpl->display($this->MAIN_JSON_TEMPLATE);
	}

	protected function GetActiveUserId()
	{
		if(isset($_SESSION))
		{
			$uDto = $_SESSION[SESSION_USER_OBJECT];
			if($uDto != null)
			return $uDto->getId();
			else
			return -1;
		}
		else
		{
			return -1;
		}
	}

	protected function showBlock($template) {
		$this->smartyImpl->display($template);
	}

	protected function assign($name,$value) {
		$this->smartyImpl->assign($name,$value);
	}

	public function showBadCmd() {
	if($USE_JSON) {
			$this->setRenderType(VS_PLUGIN);
			$this->setTemplate('error/bad_json.tpl');
		}
		else {
			$this->setTemplate('error/bad.tpl');
		}
		
		$this->show();
	}

	 
}
?>